package com.moshang.blog.controller.back.blog;

import com.google.common.collect.Maps;
import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.entity.TbSort;
import com.moshang.blog.service.TbSortService;
import com.moshang.blog.core.utils.RestResponse;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @program: blog
 * @description: 分类控制器类
 * @author: xieweiwei
 * @create: 2018-11-15 18:10
 **/
@Controller
@RequestMapping("/back/blog/sort")
public class TbSortController {

    @Autowired
    private TbSortService tbSortService;
    /**
     * 进入分类页面
     */
    @RequestMapping("list")
    public String showMenuList(){

        return "back/blog/sort/list";
    }

    @ApiOperation(value = "/treelist", httpMethod = "POST", notes = "展示分类")
    @RequiresPermissions("blog:sort:list")
    @RequestMapping("treelist")
    @ResponseBody
    public RestResponse treelist(){
        Map<String,Object> map = Maps.newHashMap();
        map.put("parentId",0);
        return RestResponse.success().setData(tbSortService.selectAllSorts(map));
    }
    /**
     * 进入添加分类/顶级分类页面
     * @param parentId
     * @param model
     * @return
     */

    @GetMapping("addUI")
    public String addUI(@RequestParam(value = "parentId",required = false) Long parentId, Model model){
        if(parentId != null){
            TbSort tbSort = tbSortService.getById(parentId);
            model.addAttribute("tbSort",tbSort);
        }
        return "back/blog/sort/add";
    }

    @ApiOperation(value = "/add", httpMethod = "POST", notes = "添加分类")
    @RequiresPermissions("blog:sort:add")
    @PostMapping("add")
    @ResponseBody
    @SystemLog("保存分类数据")
    public RestResponse add(TbSort tbSort){
        String type="INSERT";
        if(StringUtils.isBlank(tbSort.getName())){
            return RestResponse.failure("分类名称不能为空");
        }
        if(tbSortService.getCountByName(tbSort.getName())>0){
            return RestResponse.failure("分类名称已存在");
        }
        if (tbSort.getParentId()==null){
            tbSort.setParentId(0);
        }else {
            TbSort parentSort = tbSortService.getById(tbSort.getParentId());
            if (parentSort == null) {
                return RestResponse.failure("父分类不存在");
            }
        }
        tbSortService.saveOrUpdateSort(tbSort,type);
        return RestResponse.success();
    }

    /**
     * 进入修改分类页面
     * @param id
     * @param model
     * @return
     */

    @GetMapping("editUI")
    public String editUI(@RequestParam(value = "id") Long id, Model model){

            TbSort tbSort = tbSortService.getById(id);
            model.addAttribute("tbSort",tbSort);
        return "back/blog/sort/edit";
    }

    @RequiresPermissions("blog:sort:edit")
    @PostMapping("edit")
    @ResponseBody
    @SystemLog("保存编辑分类数据")
    @ApiOperation(value = "/edit", httpMethod = "POST", notes = "修改分类")
    public RestResponse edit(TbSort tbSort){
        String type="update";
        if(tbSort.getId() == null){
            return RestResponse.failure("分类ID不能为空");
        }
        if (StringUtils.isBlank(tbSort.getName())) {
            return RestResponse.failure("菜单名称不能为空");
        }
        TbSort oldTbsort = tbSortService.getById(tbSort.getId());
        if(oldTbsort.getName().equals(tbSort.getName())) {
                return RestResponse.failure("菜单名称已存在");
        }

        if(tbSort.getSort() == null){
            return RestResponse.failure("排序值不能为空");
        }
        tbSortService.saveOrUpdateSort(tbSort,type);
        return RestResponse.success();
    }
    /**
     * 物理删除
     * @param id
     * @return
     */
    @RequiresPermissions("blog:sort:delete")
    @PostMapping("delete")
    @ResponseBody
    @SystemLog("删除分类")
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "删除分类")
    public RestResponse delete(@RequestParam(value = "id")Integer id){
        if(id == null){
            return RestResponse.failure("ID不能为空");
        }
        //查询该分类下是否有子分类
        List<TbSort> list=tbSortService.selectByParent(id);
        for (TbSort tbSort:list){
            tbSortService.removeById(tbSort.getId());

        }
        return RestResponse.success();
    }


}
